// pages/collectCard/collectCard.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      numArray: ['一次最多集一点', '任意点数'],
      numIndex: 0,
      info: {
          name: '',
          shopId: app.globalData.shopId,
          validityLowerBound: '',
          validityUpperBound: '',
          timezoneLowerBound: '',
          timezoneUpperBound: '',
          maxPoint: '',
          maxAddPoint: 1,
          rewardCouponPoolId: '',
          collectRules: '',
          restrictRules: '',
      },
      couponPoolIds: [],
      couponPoolId: ''
  },
  navTicket(){
    wx.navigateTo({
        url: `/pages/ticketsChoose/ticketsChoose?type=choose&couponPoolIds=${JSON.stringify(this.data.couponPoolIds)}&delivery=${JSON.stringify({ info: this.data.info, numIndex: this.data.numIndex})}`,
    })
  },
  numPickerChange(e){
      let index = e.detail.value
      if (index == 0){
          this.data.info.maxAddPoint = 1
      }
      if (index == 1){
          this.data.info.maxAddPoint = this.data.info.maxPoint
      }
      this.setData({
          info: this.data.info,
          numIndex: e.detail.value
      })
  },
  iName(e){
      this.data.info.name = e.detail.value
      this.setData({
            info: this.data.info
      })
  },
  pvlb(e){
      this.data.info.validityLowerBound = e.detail.value
      this.setData({
          info: this.data.info
      })
  },
  pvub(e){
      this.data.info.validityUpperBound = e.detail.value
      this.setData({
          info: this.data.info
      })
  },
  icollectRules(e){
      this.data.info.collectRules = e.detail.value
      this.setData({
          info: this.data.info
      })
  },
  ptimezoneLowerBound(e){
      this.data.info.timezoneLowerBound = e.detail.value
      this.setData({
          info: this.data.info
      })
  },
  ptimezoneUpperBound(e){
      this.data.info.timezoneUpperBound = e.detail.value
      this.setData({
          info: this.data.info
      })
  },
  irestrictRules(e){
      this.data.info.restrictRules = e.detail.value
      this.setData({
          info: this.data.info
      })
  },
  imaxPoint(e){
      this.data.info.maxPoint = e.detail.value
      let index = this.data.numIndex
      if (index == 0) {
          this.data.info.maxAddPoint = 1
      }
      if (index == 1) {
          this.data.info.maxAddPoint = this.data.info.maxPoint
      }
      this.setData({
          info: this.data.info
      })
  },
    create(){
        console.log(this.data.info)
        let resultObj = JSON.parse(JSON.stringify(this.data.info))
        resultObj.rewardCouponPoolId = this.data.couponPoolId
        if (Object.values(resultObj).filter(val => String(val).trim() === '').length){
            wx.showModal({
                title: '提示',
                content: '尚有信息未填写',
                showCancel: false
            })
            return
        }
        resultObj.collectRules = JSON.stringify({ context: this.data.info.collectRules})
        resultObj.restrictRules = JSON.stringify({ context: this.data.info.restrictRules})
        
        app.globalData.post({
            url: '/pointCard/create',
            data: resultObj,
            success: (data) => {
                // wx.navigateBack({
                    
                // })
            }
        })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //   this.init(options)
  },
    // init(options){
    //     if (options.couponPoolIds) {
    //         let delivery = JSON.parse(options.delivery)
    //         console.log(this.data.info, delivery)
    //         this.setData({
    //             couponPoolIds: JSON.parse(options.couponPoolIds),
    //             couponPoolId: options.couponPoolId,
    //             numIndex: +delivery.numIndex,
    //             info: Object.assign(this.data.info, delivery.info)
    //         })
    //     }
    // },
    setCouponPool({ couponPoolIds, couponPoolId}){
        this.setData({
            couponPoolIds,
            couponPoolId
        })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})